/**
 *******************************************************************************
 * @FileName  : tlsr825x_uart.h
 * @Author    : GaoQiu
 * @CreateDate: 2020-09-10
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */

#ifndef TLSR825X_UART_H_
#define TLSR825X_UART_H_

#include "tlsr825x.h"

/*!  UART Initialization type */
typedef struct{
	uint32_t SysClkFreq;

	uint32_t UART_BaudRate;

	uint8_t  UART_Parity;
	uint8_t  UART_StopBits;
	uint8_t  UART_HwFlowCtrl; /*!< unused currently. */
}UART_InitType_t;


/*!  UART_Parity */
#define UART_Parity_No     ((uint8_t)0x00)
#define UART_Parity_Odd    ((uint8_t)0x04)
#define UART_Parity_Even   ((uint8_t)0x0C)
#define IS_UART_PARITY(PARITY) (((PARITY) == UART_Parity_No)   || \
                                ((PARITY) == UART_Parity_Even) || \
                                ((PARITY) == UART_Parity_Odd))

/*!  UART_StopBits */
#define UART_StopBits_1      ((uint8_t)0x00)
#define UART_StopBits_1_5    ((uint8_t)0x10)
#define UART_StopBits_2      ((uint8_t)0x20)
#define IS_UART_STOPBITS(STOPBITS) (((STOPBITS) == UART_StopBits_1) || \
                                    ((STOPBITS) == UART_StopBits_2) || \
                                    ((STOPBITS) == UART_StopBits_1_5))

/*!  UART_HwFlowCtrl */
#define UART_HwFlowCtrl_None        ((uint16_t)0x0000)
#define UART_HwFlowCtrl_RTS         ((uint16_t)0x8000)
#define UART_HwFlowCtrl_CTS         ((uint16_t)0x0002)
#define UART_HwFlowCtrl_RTS_CTS     ((uint16_t)0x8002)
#define IS_UART_HW_FLOW_CTRL(CONTROL) (((CONTROL) == UART_HwFlowCtrl_None) || \
                                       ((CONTROL) == UART_HwFlowCtrl_RTS)  || \
                                       ((CONTROL) == UART_HwFlowCtrl_CTS)  || \
                                       ((CONTROL) == UART_HwFlowCtrl_RTS_CTS))

/*!  UART TImeout */
#define UART_Timeout_1x               ((uint16_t)0x0000)
#define UART_Timeout_2x               ((uint16_t)0x0100)
#define UART_Timeout_3x               ((uint16_t)0x0200)
#define UART_Timeout_4x               ((uint16_t)0x0300)
#define IS_UART_TIMEOUT(TIMEOUT)      (((TIMEOUT) == UART_Timeout_1x) || \
		                               ((TIMEOUT) == UART_Timeout_2x) || \
		                               ((TIMEOUT) == UART_Timeout_3x) || \
                                       ((TIMEOUT) == UART_Timeout_4x)    )
/*!  UART interrupt enable bit define */
#define UART_IT_Rx                ((uint8_t)0x40)
#define UART_IT_Tx                ((uint8_t)0x80)
#define UART_IT_Rx_Tx             ((uint8_t)0xC0)

/*!  UART interrupt flag bit define */
#define UART_IT_Flag_RxDone       ((uint8_t)0x08)
#define UART_IT_Flag_RxErr        ((uint8_t)0x80)

/*!  UART Rx/Tx State */
#define UART_State_RxDone       ((uint8_t)0x04)
#define UART_State_TxDone       ((uint8_t)0x01)


/**** UART DMA Function ****/
#define UART_DMA_Rx               ((uint8_t)0x10)
#define UART_DMA_Tx               ((uint8_t)0x20)
#define UART_DMA_Rx_Tx            ((uint8_t)0x30)

/**** UART DMA Interrupt ****/
#define UART_DMA_IT_Rx            ((uint8_t)0x01)
#define UART_DMA_IT_Tx            ((uint8_t)0x02)
#define UART_DMA_IT_Rx_Tx         ((uint8_t)0x03)

/**** UART DMA Interrupt ****/
#define UART_DMA_IT_Flag_RxDone    ((uint8_t)0x01)
#define UART_DMA_IT_Flag_TxDone    ((uint8_t)0x02)
#define UART_DMA_IT_Flag_RxTxDone  ((uint8_t)0x03)

/**
 * @berif : UART initialization.
 * @param : UART_InitStruct     UART initialization structure.
 * @return: None.
 */
void UART_Init(UART_InitType_t *UART_InitStruct);

/********** No DMA mode API ***********/

/**
 * @berif : UART Interrupt configuration.
 * @param : UART_IT     can be UART_IT_Rx/UART_IT_Rx/UART_IT_Rx_Tx.
 * @param : enable      enable/disable UART interrupt.
 * @return: None.
 */
void UART_ITConfig(uint8_t UART_IT, bool_t enable);

/**
 * @berif : Get UART Interrupt state flag.
 * @param : UART_IT     can be UART_IT_Flag_RxDone/UART_IT_Flag_RxErr.
 * @return: None.
 */
uint8_t UART_GetITFlagState(uint8_t UART_IT_Flag);

/**
 * @berif : Clear UART Interrupt state flag.
 * @param : UART_IT     can be UART_IT_Flag_RxDone/UART_IT_Flag_RxErr.
 * @return: None.
 */
void UART_ClearITFlagState(uint8_t UART_IT_Flag);

/**
 * @berif : UART send a byte.
 * @param : data       data will be sent.
 * @return: None.
 */
void UART_SendByte(uint8_t data);

/**
 * @berif : UART send data.
 * @param : pData       Pointer point to buffer.
 * @param : len         the length of pData.
 * @return: None.
 */
void UART_SendData(uint8_t *pData, uint32_t len);

/**
 * @berif : UART receive data.
 * @param : None.
 * @return: return received data.
 */
uint8_t UART_ReceiveByte(void);


/*************** DMA mode API ***************/

/**
 * @berif : UART DMA mode configuration.
 * @param : pRxBuf        Pointer point to Rx buffer.
 * @param : byteLen       the length of Rx buffer.
 * @return: None.
 */
void UART_DmaConfig(uint8_t *pRxBuf, uint32_t byteLen);

/**
 * @berif : UART DMA interrupt configuration.
 * @param : UART_DMA_IT      can be UART_DMA_IT_Rx/UART_DMA_IT_Tx/UART_DMA_IT_Rx_Tx
 * @param : enable           disable/enable DMA mode
 * @return: None.
 */
void UART_DmaITConfig(uint8_t UART_DMA_IT, bool_t enable);

/**
 * @berif : UART DMA enable/disable configuration.
 * @param : UART_DMA      can be UART_DMA_Rx/UART_DMA_Tx/UART_DMA_Rx_Tx
 * @param : enable        enable/disable DMA
 * @return: None.
 */
void UART_DmaCmd(uint8_t UART_DMA, bool_t enable);

/**
 * @berif : Get UART DMA interrupt state flag.
 * @param : UART_DMA_IT_Flag      can be UART_DMA_IT_Flag_RxDone/
 *                                       UART_DMA_IT_Flag_TxDone/
 *                                       UART_DMA_IT_Flag_RxTxDone
 * @return: None.
 */
static inline uint8_t UART_GetDmaITFlagState(uint8_t UART_DMA_IT_Flag)
{
	return DMA->IRQ_STA & UART_DMA_IT_Flag;
}

/**
 * @berif : Clean UART DMA interrupt state flag.
 * @param : UART_DMA_IT_Flag      can be UART_DMA_IT_Flag_RxDone/
 *                                       UART_DMA_IT_Flag_TxDone/
 *                                       UART_DMA_IT_Flag_RxTxDone
 * @return: None.
 */
static inline void UART_ClearDmaITFlagState(uint8_t UART_DMA_IT_Flag)
{
	DMA->IRQ_STA = UART_DMA_IT_Flag;
}

/**
 * @berif : UART send data by DMA.
 * @param : pData      Pointer point to data buffer.
 *       !!!Note:      data format:
 *          LSB | data Length  |  data        | MSB
 *              |   4B         |  N bytes     |
 * @return: None.
 */
bool_t UART_SendDataByDma(uint8_t *pData);


/*************** Common API ***************/

/**
 * @berif : Check if UART is busy.
 * @param : None.
 * @return: None.
 */
bool_t UART_IsBusy(void);

#endif /* TLSR825X_UART_H_ */

